package com.meilingcloud.research.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilingcloud.common.exception.CustomException;
import com.meilingcloud.common.utils.SecurityUtils;
import com.meilingcloud.research.domain.RwsTopic;
import com.meilingcloud.research.domain.RwsTopicUser;
import com.meilingcloud.research.mapper.RwsTopicMapper;
import com.meilingcloud.research.service.IRwsTopicService;
import com.meilingcloud.research.service.IRwsTopicUserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 课题 Service业务层处理
 *
 * @author chenl
 * @date 2021-03-23
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Service
public class RwsTopicServiceImpl extends ServiceImpl<RwsTopicMapper, RwsTopic> implements IRwsTopicService {

    private final IRwsTopicUserService iRwsTopicUserService;

    @Override
    public List<RwsTopic> queryList(RwsTopic rwsTopic, List<Long> topicIds) {
        LambdaQueryWrapper<RwsTopic> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(rwsTopic.getTopicCode())) {
            lqw.eq(RwsTopic::getTopicCode, rwsTopic.getTopicCode());
        }
        if (StringUtils.isNotBlank(rwsTopic.getTopicName())) {
            lqw.like(RwsTopic::getTopicName, rwsTopic.getTopicName());
        }
        if (StringUtils.isNotBlank(rwsTopic.getTopicSource())) {
            lqw.eq(RwsTopic::getTopicSource, rwsTopic.getTopicSource());
        }
        if (CollectionUtil.isNotEmpty(rwsTopic.getStartTimeRange())) {
            lqw.between(RwsTopic::getStartTime, rwsTopic.getStartTimeRange().get(0),
                    rwsTopic.getStartTimeRange().get(1));
        }
        if (CollectionUtil.isNotEmpty(topicIds)) {
            lqw.in(RwsTopic::getTopicId, topicIds);
        }
        return this.list(lqw);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveTopic(RwsTopic rwsTopic) {
        boolean result = this.save(rwsTopic);
        RwsTopicUser topicUser = new RwsTopicUser();
        topicUser.setTopicId(rwsTopic.getTopicId());
        topicUser.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        iRwsTopicUserService.save(topicUser);
        return result;
    }

    @Override
    public boolean updateTopic(RwsTopic rwsTopic) {
        RwsTopic oldTopic = this.getById(rwsTopic);
        if (!StringUtils.equals(SecurityUtils.getUsername(), oldTopic.getCreateBy())) {
            throw new CustomException("不允许非创建人修改课题");
        }
        return this.updateById(rwsTopic);
    }

    @Override
    public boolean removeTopicById(Long topicId) {
        RwsTopic oldTopic = this.getById(topicId);
        if (!StringUtils.equals(SecurityUtils.getUsername(), oldTopic.getCreateBy())) {
            throw new CustomException("不允许非创建人删除课题");
        }
        return this.removeById(topicId);
    }

}
